/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.containers.HBox;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.TextInput;
	/**
	 * 上传文件的组件 
	 * 
	 */
	public class EDIUPFile extends HBox
	{
		private var lab:Label;
		private var secondlab:Label;
		private var txtFirstFile:TextInput;
		private var txtSecondFile:TextInput;
		private var btnFirstBrowse:Button;
		private var btnSecondBrowse:Button;
		private var firstBrowsFile:FileReference;
		private var secondBrowsFile:FileReference;
		
		/**
		 * 上传文件时的文件类型 
		 */				
		public var refType:String;
		/**
		 * 与上传文件绑定的id编号 
		 */		
		public var refId:String;
		
		/****
		 * 是否显示第二个上传按钮,默认显示第二个按钮 
		 * 由于mxml在传值时无法为该属性赋值
		 * 故当在mxml中使用该组件时,不可使用该属性.
		 * 当在as中调用构造函数时,可以使用该属性
		 */
		[Bindable]
		private var _showDoubleBtn:Boolean = true;
		
		private var upcompleteCallback:Function;
		/**
		 * 构造上传文件的布局 
		 * 
		 */		
		public function EDIUPFile(showDoubleBtn:Boolean = true)
		{
			trace("showDoubleBtn"+showDoubleBtn);
			super();
			lab=new Label();
			lab.text="附件：";
			this.addChild(lab);
			
			txtFirstFile=new TextInput();
			txtFirstFile.editable=false;
			txtFirstFile.width = 200;
			this.addChild(txtFirstFile);
			
			btnFirstBrowse=new Button();
			btnFirstBrowse.label="浏览";
			btnFirstBrowse.addEventListener(MouseEvent.CLICK,firstBrowsHandler);
			this.addChild(btnFirstBrowse);
			
			if(_showDoubleBtn){//判断是否显示第二个按钮
				trace("进入IF");
				secondlab=new Label();
				secondlab.text="附件：";
				this.addChild(secondlab);
			
				txtSecondFile=new TextInput();
				txtSecondFile.editable=true;
				txtSecondFile.width = 200;
				this.addChild(txtSecondFile);
			
				btnSecondBrowse=new Button();
				btnSecondBrowse.label="浏览";
				btnSecondBrowse.addEventListener(MouseEvent.CLICK,secondBrowsHandler);
				this.addChild(btnSecondBrowse);
			}
			
			//firstBrowsFile=new FileReference();
			//secondBrowsFile=new FileReference();
		}
		
		protected function firstBrowsHandler(event:MouseEvent):void
		{
			try
			{
				firstBrowsFile=new FileReference();
				firstBrowsFile.addEventListener(Event.SELECT,firstFileSelectHandler);	
				firstBrowsFile.browse();
			}
			catch(error:Error)
			{
				Alert.show("发生错误"+error.message);
			}
		}
		
		protected function secondBrowsHandler(event:MouseEvent):void
		{
			try
			{
				secondBrowsFile=new FileReference();
				secondBrowsFile.addEventListener(Event.SELECT,secondFileSelectHandler);	
				secondBrowsFile.browse();
			}
			catch(error:Error)
			{
				Alert.show("发生错误"+error.message);
			}
		}
		
		private function firstFileSelectHandler(event:Event):void
		{
			txtFirstFile.text=firstBrowsFile.name;
		}
		
		private function secondFileSelectHandler(event:Event):void
		{
			txtSecondFile.text=secondBrowsFile.name;
		}
		/**
		 * 上传文件 
		 * @return 
		 * 
		 */		
		public function upFile(completeCallBack:Function = null):Boolean
		{
			var ret:Boolean=true;
			var urlRequest:URLRequest;
			var serverSideScript:String="";
			this.upcompleteCallback = completeCallBack;
			try
			{ 
				if(txtFirstFile.text!="")
				{
					serverSideScript = EDIApplication.path+"commonUpload?"+"?refType="+refType+"&refId="+refId+"&appName="+EDIApplication.appName+"&fileName="+firstBrowsFile.name;
					urlRequest = new URLRequest(encodeURI(serverSideScript));
					if(firstBrowsFile.size<=0){
						mx.controls.Alert.show("不可选择空文件上传!","温馨提示");
						this.clear();
						return false;
					}
					firstBrowsFile.upload(urlRequest);
//					if(completeCallBack)
						firstBrowsFile.addEventListener(Event.COMPLETE,clearAll);
//					else
//						firstBrowsFile.addEventListener(Event.COMPLETE,completeCallBack);
				}
				
				if(_showDoubleBtn&&txtSecondFile.text!="")
				{
					serverSideScript = EDIApplication.path+"commonUpload?"+"refType="+refType+"&refId="+refId+"&appName="+EDIApplication.appName+"&fileName="+secondBrowsFile.name;
					urlRequest = new URLRequest(encodeURI(serverSideScript));
					if(secondBrowsFile.size<=0){
						mx.controls.Alert.show("不可选择空文件上传!","温馨提示");
						this.clear();
						return false;
					}
					secondBrowsFile.upload(urlRequest);
//					if(completeCallBack)
						secondBrowsFile.addEventListener(Event.COMPLETE,clearAll);
//					else
//						secondBrowsFile.addEventListener(Event.COMPLETE,completeCallBack);
				}
			}
			catch(e:Error)
			{
				//mx.controls.Alert.show(e.message);
				ret=false;
			}
			return ret;
		}
		
		
		private function clearAll(event:Event):void{
			//this.clear();
			if(upcompleteCallback!=null){
				upcompleteCallback();
			}
		}
		
		/****
		 * 当在MXML中调用该组件时,当需要显示一个上传控件时,
		 * 在init方法中调用该方法,可以删除第二个上传控件
		 */
		public function removeSecondObj():void{
			if(secondlab!=null)
				this.removeChild(secondlab);
			if(txtSecondFile!=null)
				this.removeChild(txtSecondFile);
			if(btnSecondBrowse!=null)
				this.removeChild(btnSecondBrowse);
			secondlab = null;
			txtSecondFile = null;
			btnSecondBrowse = null;
		}
		
		public function set showDoubleBtn(value:Boolean):void{
			this._showDoubleBtn = value;
		}
		public function get showDoubleBtn():Boolean{
			return this._showDoubleBtn;
		}
		/**
		 * 清空uploadFile
		 */
		public function clear():void{
			txtFirstFile.text="";
			refId = "";
			firstBrowsFile = new FileReference();
			if(_showDoubleBtn){
				if(secondlab!=null)
					txtSecondFile.text = "";
				if(btnSecondBrowse!=null)	
					secondBrowsFile = new FileReference();
			}
		}
		
		
		/**
		 * 判断当前是否选中文件
		 */
		public function hasFile():Boolean{
			if(txtFirstFile.text!=""){
				return true;
			}
			if(_showDoubleBtn&&null!=txtSecondFile){
				if(""!=txtSecondFile.text || null!=txtSecondFile.text){
					return true;
				}
			}	
			return false;
		}
		
	}
}